package cn.bon.user.service.impl;

import cn.bon.common.bean.JsonResult;
import cn.bon.common.bean.PolyAuthToken;
import cn.bon.common.enums.ResultCode;
import cn.bon.common.tool.R;
import cn.bon.common.tool.RedisUtils;
import cn.bon.common.utils.JwtUtils;
import cn.bon.user.mapper.UserMapper;
import cn.bon.user.pojo.dto.UserLoginDTO;
import cn.bon.user.pojo.po.UserPo;
import cn.bon.user.pojo.vo.UserLoginVO;
import cn.bon.user.service.UserSysOpService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: dronff
 * @description:
 * @CreateTime: 2023/12/8 9:45
 **/
@Service
public class UserSysOpServiceImpl extends ServiceImpl<UserMapper, UserPo> implements UserSysOpService {
    @Resource
    private AuthenticationManager authenticationManager;
    @Resource
    private JwtUtils jwtUtils;

    @Override
    public JsonResult<UserLoginVO> login(UserLoginDTO userLoginDTO, HttpServletResponse httpServletResponse) {
        UserPo userPo = getOne(
                new QueryWrapper<UserPo>()
                        .eq("username", userLoginDTO.getUsername())
        );
        if (userPo == null) {
            return R.fail(ResultCode.USER_CREDENTIALS_ERROR);
        }
        UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(userLoginDTO.getUsername(), userLoginDTO.getPassword());
        // 用户认证
        Authentication authenticate = null;
        try {
             authenticate = authenticationManager.authenticate(usernamePasswordAuthenticationToken);
        }catch (Exception ignored){

        }

        if (authenticate == null) {
            return R.fail(ResultCode.USER_CREDENTIALS_ERROR);
        }

        Map<String,Object> user = new HashMap<>();
        user.put("username",usernamePasswordAuthenticationToken.getName());
        user.put("authorities",usernamePasswordAuthenticationToken.getName());
        // 生成token
        String token = jwtUtils.createToken(
                user
        );
        // 响应头中添加token信息
        httpServletResponse.addHeader(
                "set-tk",token
        );
        RedisUtils.set("user:login:" + usernamePasswordAuthenticationToken.getName(),new PolyAuthToken(token,authenticate));
        UserLoginVO retUser = UserLoginVO
                .builder()
                .username(userPo.getUsername())
                .build();
        return R.success(retUser);
    }

    @Override
    public JsonResult logout(String username) {
        RedisUtils.del("user:login:" + username);
        return R.success();
    }
}
